<?php
namespace app\admin\model;
use \think\Model;
use Think\Db;

class Order extends Model {
    protected $pk = 'id';
    protected $table = 'sdb_order';
    /**
     * 订单关联订单商品模型
     * @return \think\model\relation\HasMany
     */
    public function orderGoodsInfos()
    {
        return $this->hasMany('OrderShop','order_number','order_number');
    }

    /**
     * 远程订单对商品
     * @return \think\model\relation\HasManyThrough
     */
    public function goods()
    {
        return $this->hasManyThrough('Shop','OrderShop','order_number','id','id');
    }

    /**
     * 单个订单的所有商品
     * @param $order_number
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getOrderShops($order_number)
    {
        $join = [
            ['sdb_shop b','a.shop_id = b.id'],
        ];
        return  Db::table('sdb_order_shop')->alias('a')->join($join)
            ->where('a.order_number',$order_number)
            ->select();
    }

    public function getSearchData($name)
    {
        $join = [
            ['sdb_order_shop w','a.order_number = w.order_number'],
            ['sdb_shop b','w.shop_id = b.id'],
        ];
        return Db::table('sdb_order')->alias('a')->join($join)
            ->where('b.shop_name',$name)
            ->where('a.order_use','<>',2)
            ->select();
    }

    /**
     * 订单列表页获取shop_num和
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getOrderShopNumSum()
    {
        return Db::table('sdb_order_shop')
            ->field('order_number,sum(shop_num) as numTotal')
            ->group('order_number')
            ->select();
    }
    public function numList()
    {
        $list['order'] = $this->count();
        $list['remind'] = $this->where('user_remind',1)->where('order_use','<>',2)->count();
        $list['refund'] = $this->where('order_staff',5)->whereOr('order_staff',6)->where('order_use','<>',2)->count();
        $list['pendingPay'] = $this->where('order_staff',1)->where('order_use','<>',2)->count();
        $list['pendingSend'] = $this->where('order_staff',2)->where('freight_staff',0)->where('order_use','<>',2)->count();
        $list['abnormal'] = $this->where('order_use',2)->count();
        $list['finish'] = $this->where('order_staff',3)->where('order_use','<>',2)->count();
        $list['close'] = $this ->where('order_staff',4)->where('order_use','=',1)->count();
        return $list;
    }


}